#include <stdlib.h>
#include <stdio.h>
#include <cogent.h>

#include "plcdb.h"

int plcdb_flush ()
{
	IP_Msg			*hmsg;
	IP_Task			*myself;
	IP_Task			*hist;
	char			data [512];
	int				len;
	
	if (!(myself = IP_TaskCreateMe (IP_GetChannelID(), "ph_view", NULL, NULL, 0)))
	{
	    fprintf (stderr, "Could not initialize Cascade IPC subsystem\n");
	}
	hmsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);
	hist = IP_NserveLookupName ("/plcdb");
	
	len = sprintf (data, "flush") + 1;
	IP_MsgCascade (hmsg, data, len, IP_NONE, ST_OK);
	IP_TaskSendSync (myself, hist, hmsg, hmsg);
	if (!strcmp ((char*) hmsg->msg->data, "ERROR")) {
		printf ("flush reply: %s\n", (char*) hmsg->msg->data);
		return -1;
	}	
	
	IP_MsgDestroy (hmsg);
	IP_TaskDestroy (myself);
	IP_TaskDestroy (hist);
	return 0;
}

int plcdb_start_new_file ()
{
	IP_Msg			*hmsg;
	IP_Task			*myself;
	IP_Task			*hist;
	char			data [512];
	int				len;
	
	if (!(myself = IP_TaskCreateMe (IP_GetChannelID(), "ph_view", NULL, NULL, 0)))
	{
	    fprintf (stderr, "Could not initialize Cascade IPC subsystem\n");
	}
	hmsg = IP_MsgCreate (NULL, IP_MsgDefaultSize(), 0);
	hist = IP_NserveLookupName ("/plcdb");
	
	len = sprintf (data, "start_new_file") + 1;
	IP_MsgCascade (hmsg, data, len, IP_NONE, ST_OK);
	IP_TaskSendSync (myself, hist, hmsg, hmsg);
	if (!strcmp ((char*) hmsg->msg->data, "ERROR")) {
		printf ("start_new_file reply: %s\n", (char*) hmsg->msg->data);
		return -1;
	}	
	
	IP_MsgDestroy (hmsg);
	IP_TaskDestroy (myself);
	IP_TaskDestroy (hist);
	return 0;
}
